import logging
import os
import time


class logger:
    # 使用注解表示这是一个类方法
    @classmethod
    def getlog(cls):
        # 获取日志对象并设置等级
        cls.logger = logging.getLogger(name=__name__)
        cls.logger.setLevel(logging.DEBUG)

        # 配置文件路径
        path = "./logs/"

        # 校验一下，防止目录不存在
        if not os.path.exists(path):
            os.mkdir(path)

        # 设置日期格式
        date = time.struct_time("%Y-%m-%d")


        # 日志路径名
        log_name = path + date + ".log"
        log_err_name = path + date + "-err.log"
        log_info_name = path + date + "-info.log"


        # 设置日志处理器
        handler = logging.FileHandler(filename=log_name,encoding="utf-8")
        errHandler = logging.FileHandler(filename=log_err_name,encoding="utf-8")
        infoHandler = logging.FileHandler(filename=log_info_name,encoding="utf-8")
        streamHandler = logging.StreamHandler(filename=log_info_name,encoding="utf-8")

        # 设置格式并加入到处理器中
        formatter = logging.Formatter( "%(asctime)s %(levelname)s [%(name)s] [%(filename)s (%(funcName)s:%(lineno)d)] - %(message)s")
        handler.setFormatter(formatter)
        errHandler.setFormatter(formatter)
        infoHandler.setFormatter(formatter)
        streamHandler.setForatter(formatter)

        # 添加处理器
        cls.logger.addHandler(handler)
        cls.logger.addHandler(errHandler)
        cls.logger.addHandler(infoHandler)
        cls.logger.addHandler(streamHandler)

        return cls.logger